/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bice.backbean;

//<editor-fold defaultstate="collapsed" desc=" Imports ">
import com.bice.dao.Dao;
import com.bice.dao.IEntity;
import com.bice.util.FacesUtil;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Controller;
import sun.awt.AppContext;
//</editor-fold>

/**
 *
 * @author Yeni
 */
public class BackBean implements Serializable {

    public static final String SESSION_BEAN = "sessionBean";
    public static final String MESSAGES_PROXY_BEAN = "messagesProxyBean";
    public static final String SESSION_BEAN_USER = "user";
    
    //<editor-fold defaultstate="collapsed" desc=" Constructor ">
    /**
     * Create an instance of Back bean
     */
    public BackBean() {
    }
    //</editor-fold>

    /**
     * Get EL context value
     * @param bean Bean to get
     * @param property Property to get from bean
     * @return Value of property
     */
    public Object getELValue(String bean, String property) {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getApplication().evaluateExpressionGet(context, String.format("#{%s.%s}", bean, property), Object.class);
    }

    /**
     * Encrypt an string
     * @param value Value to encrypt
     * @return Value encripted
     */
    public String encrypt(String value) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(BackBean.class.getName()).log(Level.SEVERE, null, ex);
            while(value.length() < 32) {
                value += value;
            }
            if(value.length() >= 32){
                value = value.substring(0, 32);
            }
            return value;
        }
        byte[] b = md.digest(value.getBytes());
        int size = b.length;
        StringBuilder h = new StringBuilder(size);
        for (int i = 0; i < size; i++) {
            int u = b[i] & 255;
            if (u < 16) {
                h.append("0").append(Integer.toHexString(u));
            } else {
                h.append(Integer.toHexString(u));
            }
        }
        return h.toString();
    }

    /**
     * @return The session bean
     */
    public SessionBean getSessionBean() {
        return (SessionBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(SESSION_BEAN);
    }
    
    /**
     * @return The session messages proxy bean
     */
    public MessagesProxyBean getMessagesProxyBean() {
        return (MessagesProxyBean) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(MESSAGES_PROXY_BEAN);
    }
    
    public Object getSpringBean(Class clazz) {
        ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        String name = clazz.getSimpleName();
        name = name.substring(0, 1).toLowerCase() + name.substring(1);
        return FacesUtil.getSpringBean(context, name);
    }
    //</editor-fold>

}